package com.zymail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyAdmin;
import com.zymail.bean.ZyGet;
import com.zymail.bean.ZyOrder;
import com.zymail.bean.ZyUser;
import com.zymail.service.ZyAdminService;
import com.zymail.service.ZyGetService;
import com.zymail.service.ZyUserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController{
    @Autowired
    ZyAdminService adminService;

    @Autowired
    ZyGetService getService;

    @Autowired
    ZyUserService userService;

    /**
     * 判断用户是否是进销商
     * @param session_key   session_key
     * @return  admin的id或者-1(失败结果)
     */
    @GetMapping("/isadmin/{session_key}")
    public Integer isAdmin(@PathVariable("session_key") String session_key){
        Integer userId = get_userId(session_key);
        if(userId == 0) return -1;
        return adminService.isAdmin(userId);
    }

    /**
     * 得到经销商信息
     * @param adminId   adminId
     * @return  map<string, string></>
     */
    @GetMapping("/info/{adminId}")
    public HashMap<String, String> getinfo(@PathVariable("adminId") Integer adminId){
        ZyAdmin admin = adminService.getById(adminId);
        if(admin == null) return  null;
        HashMap<String, String> info = new HashMap<>();
        info.put("name", admin.getName());
        info.put("avator", admin.getUser().getAvatar());
        info.put("order_count", adminService.getOrderCount(adminId) + "");
        info.put("child_count", adminService.getmenCount(adminId) + "");
        info.put("money_count", adminService.getSumCount(adminId) + "");

        return info;
    }

    /**
     * 得到所有订单信息
     * @param adminId   admin's id
     * @return  List<Zyget></>
     */
    @GetMapping("/getorder/{adminId}")
    public List<ZyGet> getOrder(@PathVariable("adminId") Integer adminId){
        QueryWrapper<ZyGet> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id", adminId);
        wrapper.orderByDesc("id");
        return getService.list(wrapper);
    }

    /**
     * 得到所有儿子
     * @param adminId   adminId
     * @return  List<ZyUser></>
     */
    @GetMapping("/getchild/{adminId}")
    public List<ZyUser> getUser(@PathVariable("adminId") Integer adminId){
        ZyAdmin admin = adminService.getById(adminId);
        QueryWrapper<ZyUser> wrapper = new QueryWrapper<>();
        wrapper.eq("fa_id", admin.getUserId());

        List<ZyUser> userList = userService.list(wrapper);
        for (ZyUser user: userList) {
            user.setOpenid(null);
            user.setDefFloor(null);
        }

        return userList;
    }
}
